//
// Created by hugan on 2021/8/30.
//

#include "MouseUtil.h"

WINBOOL MouseUtil::move(int x, int y) {
    //https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setcursorpos
    return SetCursorPos(x, y);
}

int MouseUtil::wheel(int value, bool isHorizontal) {
    //mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 300, 0);//过时方法

    /*
     * 方向
     * 水平时应该是MOUSEEVENTF_HWHEEL,但编译不通过,所以直接使用源码值,看源码!
     */
    DWORD dwFlags = isHorizontal ? 0x01000 : MOUSEEVENTF_WHEEL;
    /*
     * 距离
     * 原始api的mouseData,竖直时的值是反过来的
     * WHEEL_DELTA, MOUSEEVENTF_WHEEL,//上
     * static_cast<DWORD>(-WHEEL_DELTA), MOUSEEVENTF_WHEEL,//下
     * static_cast<DWORD>(-WHEEL_DELTA), 0x01000,//左
     * WHEEL_DELTA, 0x01000,//右
     */
    DWORD mouseData;
    if (!isHorizontal) {
        mouseData = -WHEEL_DELTA * value;
    } else {
        mouseData = WHEEL_DELTA * value;
    }

    INPUT inputs[1];
    inputs[0].type = INPUT_MOUSE;
    inputs[0].mi = {
            0, 0,
            mouseData, dwFlags
    };
    return SendInput(1, inputs, sizeof(INPUT));
}

WINBOOL MouseUtil::showMouse() {
    INPUT inputs[1];
    inputs[0].type = INPUT_MOUSE;
    inputs[0].mi = {
            1, 0,
            0, MOUSEEVENTF_MOVE
    };
    return SendInput(1, inputs, sizeof(INPUT));
}
